package com.zscat.mallplus.controller;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.single.ApiBaseAction;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.AppletLoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会员登录注册管理Controller
 * https://github.com/shenzhuan/mallplus on 2018/8/3.
 */
@Data
@RestController
@Api(tags = "AppletMemberController", description = "小程序登录首页")
@RequestMapping("/api/applet")
@Slf4j
public class AppletMemberController extends ApiBaseAction {
    @Autowired
    private IUmsMemberService memberService;

    @IgnoreAuth
    @ApiOperation("注册")
    @SysLog(MODULE = "applet", REMARK = "小程序注册")
    @PostMapping("login_by_weixin")
    public Object loginByWeixin(@RequestBody AppletLoginParam param) {
        return memberService.loginByWeixin(param);
    }


    @IgnoreAuth
    @ApiOperation("头条登录")
    @SysLog(MODULE = "applet", REMARK = "头条授权登录")
    @PostMapping("loginByToutiao/{authCode}")
    public Object loginByToutiao(@PathVariable String authCode, @RequestParam Long storeId) {
        log.info("授权code:{}", authCode);
        // 1. 服务端获取access_token、user_id
        try {
            return memberService.loginByToutiao(authCode,storeId);
        } catch (Exception e) {
            log.error("头条登录失败.异常:{}", e.getMessage());
            return new CommonResult().failed("头条授权登录失败");
        }
    }



    // 服务端获取access_token、user_id
    private AlipaySystemOauthTokenResponse getAccessToken(String authCode) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                "2021001154690187",                    // 1. 填入appid
                "MIIEvQIBADANBgkqhkiG9w0BAQ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HcWAdNX74TmrMk7BELnZ8ZZ0RVWv6f6wWn/LIJWXvTv2lBpa4vLI+S3jpRrf6cRXO67P3TW7/Dp8xd3vQgk8u8AfwgU5gYHVzkzJpoMpoMHz5Iz/K85CW4oN6s6fXgKPZr5ckfQVeNXuz7WwVIDwBsaOWrCtmVesPa6lCAAExHLAoGARujLeEIfoQriYk4ZInpK+NfGeNwY5hf1NaQk9cV7xTyzQnhHdFnRQdf8W568sSvGBk5HyID8hScy6VAj+j4+u2xwxeG7Qpg9GMCiV+OkE+GwlegYX2sbwZ62Wc0HFyImvzi7qqtHlzw5KLixAwSYX9mAPIxcB869NZZ5FjylBq0CgYEAywKeepz2Ef7nNHXYNe1qOetL9ooJK6GRzpkluWso7AM9NQHmrS0DigCHkJkT5X1IPXJejV/oXDJL4PKfKeCHj1G0wFHXiT88MR+vA9lDnYaGiD/v6NHl1okhr0kz0THczSfMEd39dOC9EK3o48zj9+ZWUGXVUsbvLmQGpREcI38=",            // 2. 填入私钥
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkk4L9JvFLSQZlHS61BMTjoGX12LQNUjYQ+X2+/W9+RorUth+G+GDJLnP3wpJp66tPr2oHzLdZOlu0AglZhlJFKZincK9NCLZ+uiRdIcqJ7oTvLyCXAyhqEO2EzAUQu6JkqYlYVDJF4dF3VIq3wFublATXg99zCEGEPFt90RXZNwkBWZFCTMKTluYHWCOEUBSE12VR7jYo5r/LzXhOyu4O8vzusjcENWq5sD8sT9yy4KVin4rPv4OR48o7rxKANbvs4g9uViIsM2D6ttgUmXJFYWyX4mxznPch1La9dFBkOS1QCmYOIbndCYCR+5K9B8S3R1f+F0pwAYKXXlA5ckeKwIDAQAB",         // 3. 填入公钥
                "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(authCode);        // 4. 填入前端传入的授权码authCode
        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");    // 0. 不用管
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);

        return response;
    }
}
